package com.ruoyi.app.controller;

import com.ruoyi.app.common.ServerConfig;
import com.ruoyi.app.common.jwt.TokenService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.domain.app.Device;
import com.ruoyi.system.domain.app.Feedback;
import com.ruoyi.system.domain.app.User;
import com.ruoyi.system.mapper.app.AdvertisingMapper;
import com.ruoyi.system.mapper.app.DeviceMapper;
import com.ruoyi.system.mapper.app.FeedbackMapper;
import com.ruoyi.system.mapper.app.UserManualMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@Api(tags = "通用相关")
@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    ServerConfig serverConfig;
    @Autowired
    AdvertisingMapper advertisingMapper;
    @Autowired
    FeedbackMapper feedbackMapper;
    @Autowired
    DeviceMapper deviceMapper;
    @Autowired
    TokenService tokenService;
    @Autowired
    UserManualMapper userManualMapper;
    /**
     * 通用上传请求
     */
    @ApiOperation("上传对象")
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    @ApiOperation("获取广告轮播图")
    @GetMapping("/ad-list")
    public AjaxResult getAdList(@RequestParam String appId){
        return AjaxResult.success(advertisingMapper.adList((appId)));
    }

    @ApiOperation("故障上报")
    @PostMapping("/fault-submit")
    public AjaxResult faultSubmit(@RequestParam String appId,@RequestParam String deviceNum,@RequestParam String content,String phone){
        User user = tokenService.getUser(ServletUtils.getRequest());
        Device device = deviceMapper.selectByNum(deviceNum);
        if (StringUtils.isNull(device)) {
            throw new ServiceException("设备不存在！");
        }
        Feedback feedback = new Feedback();
        feedback.setDeviceName(device.getDeviceName());
        feedback.setDeviceNum(device.getDeviceNum());
        feedback.setDeviceId(device.getId());
        feedback.setUserId(user.getId());
        feedback.setUserName(user.getUsername());
        feedback.setContent(content);
        feedback.setPhone(phone);
        feedback.setAppId(appId);
        feedback.setCreateTime(new Date());
        feedbackMapper.insertSelective(feedback);
        return AjaxResult.success();
    }

    @ApiOperation("问题反馈")
    @PostMapping("/issue-feedback")
    public AjaxResult issueFeedback(@RequestParam String appId,@RequestParam String phone,@RequestParam String content){
        User user = tokenService.getUser(ServletUtils.getRequest());
        Feedback feedback = new Feedback();
        feedback.setUserId(user.getId());
        feedback.setUserName(user.getUsername());
        feedback.setContent(content);
        feedback.setPhone(phone);
        feedback.setAppId(appId);
        feedback.setFeedbackType(2);
        feedback.setCreateTime(new Date());
        feedbackMapper.insertSelective(feedback);
        return AjaxResult.success();
        
    }

    @ApiOperation(value = "获取用户协议")
    @PutMapping("/proto/get")
    public AjaxResult getProto(String appId){
        return AjaxResult.success(userManualMapper.selectByAppid(appId));
    }


    @ApiOperation(value = "获取用户帮助")
    @PutMapping("/help/get")
    public AjaxResult getHelp(@RequestParam String appId){
        return AjaxResult.success(userManualMapper.getHelp(appId));
    }



}
